$(function() {

	var dataId = $(":hidden[name='model.id']").val();
	$.each($("[name='uploadify']"), function(i, item){
		let $this = $(item);
		let uploadUrl = $this.data('uploadurl');
		let deleteUrl = $this.data('deleteurl');
		$this.uploadify({
			auto : true,
			buttonText : "选择文件",
			width : 90,
			height : 30,
			uploader : uploadUrl + "?ver=" + Math.random(),
			swf : $("#url").val() + "/js/lib/uploadify/uploadify.swf?ver=" + Math.random(),
			fileTypeExts : "*",
			fileTypeDesc : "请选择文件",
			fileSizeLimit : "1GB",
			fileObjName : "uploadify",
			onUploadStart : function(file) {
				$this.uploadify("settings", "formData", {
					"dataId" : dataId
				});
			},
			onUploadSuccess : function(file, data, response) {
				var json = jQuery.parseJSON(data);
				var downloadUrl = $("#url").val() + '/' + $("form").attr("id") + '/download?' 
					+ 'fileName=' + file.name + '&path=' + json.path ;
				var str = '<input type="hidden" name="imgIds" value="' + json.imgId + '" /> ';
				str += '<a href="'+ downloadUrl +'" ><span>' + file.name + '</span></a>';
				if (dataId == null) {
					str += ' <a href="#" class="remove_img">删除</a>';
				} else {
					str += ' <a href="' + deleteUrl + '/' + json.imgId + '" class="confirm">删除</a>';
				}
				$("#"+item.id).parent().find('div').last().append(str);
			}
		});
	});

	

	$(".container-fluid").on("click", ".remove_img", function() {
		$(this).prev().prev().remove();
		$(this).prev().remove();
		$(this).remove();
		return false;
	});

//	$("#file_upload").on("click", function() {
//		$("#uploadify").uploadify('upload', '*');
//		return false;
//	});
//
//	$("#file_cancel").on("click", function() {
//		$("#uploadify").uploadify('cancel', '*');
//		return false;
//	});

	//flash插件判断 
	hasFlash();
	
});

function hasFlash(){
	var FlashObject = (function(){
		  if(typeof window.ActiveXObject != "undefined"){
		    return new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		  }else{
		    return navigator.plugins['Shockwave Flash'];
		  }
	})();
	
	if (!FlashObject){
		$("[name='uploadify']").on("click", function(){
			event.preventDefault();
			return false;
		});
		$("[name='uploadify']").after("<span><font color='red'>您的浏览器需允许flash运行</font></span>");
	}
}